<?php

class PhotoModel extends Model{
	private $id;
	private $id_note;
	private $id_geo;
	public $date;
	public $filename;
	public $order;//ordering file for a customized display
	
	//public format;//array with format?
	
	function PhotoModel(){
		// Call the Model constructor
		parent::Model();
	}

	public function getPhoto($id)
	{
		$this->id = $id;
		$query = $this->db->get_where('photos',array('id'=>$id));
		$photo = $query->result();
		return $photo['0'];		
	} 
	
	public function getPhotosFromNote($id_note){
		$query = $this->db->order_by('id','desc')->get_where('photos',array('id_note'=>$id_note));
		$photos = $query->result();
		return $photos;
	}
	

	public function insertPhoto($file_name, $id){
		$this->load->database();

		//$str = $this->db->insert_string('photos', array('filename' => $file_name));	
		$this->db->insert('photos', array('filename' => $file_name, 'id_note'=> $id));		
		$num = mysql_insert_id();
		return ''.$num;
		
	}
	public function deletePhoto($photo_id){
		$this->db->delete('photos', array('id' => $photo_id)); 
	}
	
	
	public function editGeo(){
		if(count($_POST) > 0) {
			$id_photo = $_POST['id_photo'];
			$adresse_photo = $_POST['adresse_photo'];
			$x_photo = $_POST['x_photo'];
			$y_photo = $_POST['y_photo'];
			
			$query = $this->db->get_where('photos',array('id'=>$id_photo));
			$photo = $query->result();
			
			$id_geo = $photo['0']->id_geo;
			
			
			if($id_geo == NULL){
				$this->db->insert('geo', array('id_photo' => $id_photo , 'address' => $adresse_photo , 'x' => $x_photo, 'y' => $y_photo));
				$num = $this->db->insert_id();
				$this->db->update('photos', array('id_geo' => $num) ,array('id' => $id_photo) );
			}
			else{
				$this->db->update('geo', array('address' => $adresse_photo , 'x' => $x_photo , 'y' => $y_photo ) ,array('id' => $id_geo) );
			}
			
		}
	}
	

}
?>